package domain;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/***
 * 分页查询配置信息类
 * 
 * @author gongkai
 * @param <T>
 *            实体类型
 * @配置 now 要查询得页码[可选，default:1] size 每页显示个数[可选，default:10] whereSql where语句[可选]
 *     whereParameter where参数值[可选] orderItems 排序,其key为排序的字段，value为排序的方式
 */
public class Page<T> {
	/***
	 * 第一页
	 */
	public final static Integer FIRST=1;
	
	private List<T> result;// 结果集
	private Integer currentPage = FIRST;// 要查询得页
	private Integer size = 10;// 每页显示个数
	private Long totalRow;// 总记录数
	private String whereSql = "";// where语句
	private Object[] whereParameter;// where参数值
	private LinkedHashMap<String, String> orderItems;// 排序
	private Integer showTotalPage=20;//最多显示多少个页码

	public Integer getShowTotalPage() {
		return showTotalPage;
	}

	public void setShowTotalPage(Integer showTotalPage) {
		this.showTotalPage = showTotalPage;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		if(currentPage==0){
			this.currentPage=1;
			return;
		}
		this.currentPage = currentPage;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size<=0 || size==null?10:size;
	}

	public Long getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(Long totalRowRow) {
		this.totalRow = totalRowRow;
	}

	public String getWhereSql() {
		return whereSql;
	}

	public void setWhereSql(String whereSql) {
		this.whereSql = whereSql;
	}

	public Object[] getWhereParameter() {
		return whereParameter;
	}
	/**
	 * 不能为集合
	 * @param whereParameter
	 */
	public void setWhereParameter(Object... whereParameter) {
		this.whereParameter = whereParameter;
	}

	public LinkedHashMap<String, String> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(LinkedHashMap<String, String> orderItems) {
		this.orderItems = orderItems;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}
	
	public Page() {
	}

	/***
	 * 构造方法:针对只设置要查询页码
	 * @param currentPage	页码
	 */
	public Page(Integer currentPage) {
		this.setCurrentPage(currentPage);
	}
	/***
	 * 构造方法:针对只设置要查询页码与每页显示记录数
	 * @param currentPage  页码
	 * @param size	每页显示记录数
	 */
	public Page(Integer currentPage, Integer size) {
		this.setCurrentPage(currentPage);
		this.size = size;
	}
	
	/***
	 * 有条件查询的
	 * @param currentPage
	 * @param size
	 * @param whereSql
	 * @param whereParameter
	 */
	public Page(Integer currentPage, Integer size, String whereSql,
			Object[] whereParameter) {
		this.setCurrentPage(currentPage);
		this.size = size;
		this.whereSql = whereSql;
		this.whereParameter = whereParameter;
	}

	
	
	
	
	/***
	 * 获得总记录数
	 * 
	 * @return
	 */
	public Long getRowCount() {
		return totalRow;
	}

	/***
	 * 获得总页数
	 * 
	 * @return
	 */
	public Integer getTotalPage() {
		return (Integer.valueOf(totalRow.toString()) + size - 1) / size;
	}

	/***
	 * 获得结果集
	 * 
	 * @return
	 */
	public List<T> getResult() {
		return result;
	}

	/***
	 * 第一页
	 * 
	 * @return
	 */
	public Integer getFirstPage() {
		return FIRST;
	}

	/***
	 * 上一页
	 * 
	 * @return
	 */
	public Integer getPreviousPage() {
		return currentPage <= 1 ? 1 : currentPage - 1;
	}

	/***
	 * 下一页
	 * 
	 * @return
	 */
	public Integer getNextPage() {
		return currentPage >= getTotalPage() ? getTotalPage() : currentPage + 1;
	}

	/***
	 * 最后一页
	 * 
	 * @return
	 */
	public Integer getLastPage() {
		return getTotalPage();
	}

	/**
	 * 获得开始页码
	 * @return
	 */
	public Integer getStartIndex() {
		Integer strartIndex=this.currentPage;
		if(this.currentPage+this.showTotalPage>this.getTotalPage() && this.currentPage<this.showTotalPage){
			strartIndex=FIRST;
		}else if(this.currentPage>this.showTotalPage){
			Integer temp=this.getTotalPage()-this.currentPage-this.showTotalPage;
			if(temp<0){
				strartIndex=this.currentPage+temp;
			}
		}
		return strartIndex;
	}


	/**
	 * 获得结束页码
	 * @return
	 */
	public Integer getEndIndex() {
		Integer endIndex=getStartIndex()+this.showTotalPage;
		return endIndex>this.getTotalPage()?this.getTotalPage():endIndex;
	}

	@Override
	public String toString() {
		return "Page [result=" + result + ", currentPage=" + currentPage
				+ ", size=" + size + ", totalRow=" + totalRow + ", whereSql="
				+ whereSql + ", whereParameter="
				+ Arrays.toString(whereParameter) + ", orderItems="
				+ orderItems + ", showTotalPage=" + showTotalPage + "]";
	}
}
